// import wuxToast from '../../component/toast/toast'

var app = getApp()
Page({
    data: {
        /** 
            * 页面配置 
            */
        winWidth: 0,
        winHeight: 0,
        // tab切换  
        currentTab: 0,
        list: [],
        hasNext: true,
    },
    onShow: function(options) {
        loadData(this, false);
    },
    onLoad: function (options) {
        var that = this;
        /** 
         * 获取系统信息 
         */
        swan.getSystemInfo({
            success: function (res) {
                that.setData({
                    winWidth: res.windowWidth,
                    winHeight: res.windowHeight
                });
            }
        });
        if(options.tab) {
            this.setData({currentTab: Number(options.tab)})
        }
        // loadData(this, false);
    },

    onPullDownRefresh: function () {
        /*
        wx.request({
            url: 'https://www.baidu.com',
            data: {},
            method: 'GET',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) {
                wx.stopPullDownRefresh();
            }
        })
        */
       loadData(this, false);
       console.log('刷新');
    },
    
    onReachBottom: function() {
        // Do something when page reach bottom.
         console.log('circle 下一页');
         loadData(this, true);
    },
    /*
    loadMore: function() {
        // Do something when page reach bottom.
         console.log('loadMore');
         loadData(this, true);
    },
    refesh: function() {
        // Do something when page reach bottom.
         console.log('refesh');
         loadData(this, false);
    },
    */
    /** 
       * 滑动切换tab 
       */
    bindChange: function (e) {
        var that = this;
        that.setData({ currentTab: e.detail.current });
        loadData(this, false);
    },
    /** 
     * 点击tab切换 
     */
    swichNav: function (e) {
        var that = this;
        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
        loadData(this, false);
    },

    viewOrderDetail: function(e) {
        let orderDetail = e.currentTarget.dataset.order;
        console.log(e.currentTarget.dataset.order);
        // app.global.orderDetail = e.currentTarget.dataset.order;
        swan.navigateTo({
            url: '../orderDetail/orderDetail?order='+JSON.stringify(orderDetail)
        })
    }
});

function loadData(that, next) {
    if(next) {
        if(!that.data.hasNext) {
            return;
        }
    }
    else {
        that.setData({
            list: [],
            hasNext: true
        });
    }
    
    const current = that.data.currentTab;
    const count = 10;
    let page = 1;
    if(next) {
        page = that.data.page + 1;
    }
    let start = new Date().getTime();
    swan.showLoading({
        title: '加载中...',
    });
    const url = `${app.globalData.prefix}/api/orders?type=${current}&page=${page}&count=${count}`
    swan.request({
        url: url,
        method: 'GET',
        header: {  
            token: app.globalData.token,
            channel: app.globalData.channel || ''
        },
        success: function(ret) {
            console.log(url);
            console.log('get orders > ', ret);
            if(!ret.data.status) {
                hideLoading(that, start, function() {
                    swan.showToast(
                        {
                          title: ret.data.msg,
                          duration: 1200
                        }
                      );
                });
            }
            else {
                if(next) {
                    if(ret.data.data && ret.data.data.length > 0) {
                        that.data.list.push(ret.data.data);
                        hideLoading(that, start, function() {});
                        that.setData({
                            page: page
                        });
                    }
                    else {
                        that.setData({
                            hasNext: false
                        });
                        hideLoading(that, start, function() {
                            swan.showToast(
                                {
                                  title: '没有更多数据了',
                                  duration: 1200
                                }
                              );
                        });
                    }
                }
                else {
                    that.setData({
                        list: ret.data.data,
                        page: page
                    });
                    hideLoading(that, start, function() {
                        
                    });
                }
            }
        },
        fail: function(ret) {
            hideLoading(that, start, function() {
                swan.showToast(
                    {
                      title: 'ret.data.msg',
                      duration: 1200
                    }
                  );
            });
        }
    });
}

function hideLoading(that, start, cb) {
    let end = new Date().getTime();
    let time = 1500 - (end - start);//2s
    if(time <= 0) {
        swan.hideLoading();
        cb();
    }
    else {
        setTimeout(function() {
            swan.hideLoading();
            cb();
        }, time);
    }
}